<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <title>选择收货地址</title>
    <link rel="stylesheet"  href="src/css/base.css?__version=9.4">
    <link rel="stylesheet"  href="src/css/pages.css?__version=9.4">
    <script type="text/javascript" src="src/js/jquery-1.10.1.min.js?__version=9.4"></script>
    <script type="text/javascript" src="src/js/rem.js?__version=9.4"></script>
    <script type="text/javascript" src="src/js/dropload.js?__version=9.4"></script>
    <script type="text/javascript" src="src/js/init.js?__version=9.4"></script>

	<script src="https://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
	<script src="/src/js/share.js?__version=9.4"></script>
	<script>
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?8ed2efe5b24e14f351f4d88ba2eb3462";
      var s = document.getElementsByTagName("script")[0]; 
      s.parentNode.insertBefore(hm, s);
    })();
    </script>
</head>
<body>
	<div class="init_content" style="display: none;">
    <div class="warp siteList-warp">
        <section class="siteList-box" id="htmlDataList">
            
        </section>

        <a href="addSite.html"><button class="btn btn-green">
            添加收货地址
            <img class="icon-img" src="src/img/tianjiadizhi.png">
        </button></a>

    </div>
	</div>
    <div class="init_loading">
        <img src="src/img/page_loading.gif?v=1">
    </div>
</body>
<script>
addheader("选择收货地址","javascript:history.back(-1);");
$(function(){
	
	$.ajax({
		type:'post',
		url:api_url+'/OpenAPI/v1/address/getUserAddress',
		data:{token:token},
		jsonp: 'callback',//跨域问题
		dataType:'jsonp',//跨域接收数据
		
		success:function (data){
			console.log(data)
			var htmlDataList = '';
			for(var i = 0;i<data.data.length;i++){
				htmlDataList += '<div class="siteList-item">';
				htmlDataList += '<div data-id="'+data.data[i].address_id+'" class="confirmAddress">';
				htmlDataList += '<div class="inline-block text-row2">';
				htmlDataList += '收货人：<span class="data_name">'+data.data[i].name+'</span>';
				htmlDataList += '</div>';
				htmlDataList += '<div class="inline-block text-row2">';
				htmlDataList += '联系方式：<span class="data_phone">'+data.data[i].phone+'</span>';
				htmlDataList += '</div>';
				htmlDataList += '<div class="inline-block text-row2">收货地区：<span class="data_region">'+data.data[i].region+'</span></div>';
				htmlDataList += '<div class="inline-block text-row2">收货地址：<span class="data_address">'+data.data[i].address+'</span></div>';
				htmlDataList += '</div>';
				htmlDataList += '<div class="addrss-tool">';
				htmlDataList += '<a href="javascript:;" class="tool-edit addressSave" data-id="'+data.data[i].address_id+'">';
				htmlDataList += '<img src="src/img/edit.png">编辑</a>';
				htmlDataList += '<a href="javascript:;" class="tool-del"  onclick="addressDel('+data.data[i].address_id+')">';
				htmlDataList += '<img src="src/img/del.png">删除</a>';
				htmlDataList += '</div>';
				htmlDataList += '</div>';
			}
			$("#htmlDataList").append(htmlDataList);
		},
		
		error:function (){
			
		}
	});
	
})
</script>

<script>
function addressDel(ress_id){
	if(!confirm('确认要删除吗？')){
		return;
	}
	$.ajax({
		type:'post',
		url:api_url+'/OpenAPI/V1/address/deleteUserAddress',
		data:{token:token,addressId:ress_id},
		jsonp:"callback",
		dataType:'jsonp',
		
		success:function (data){
			if(data.code == 0){
				alert('删除成功');
				window.location.href='/siteList.html';
			}else{
				alert('删除失败');
				return;
			}
		},
		
		error:function (){
			alert('网络错误');
			return;
		}
	});
}

$(document).on("click",".addressSave",function(){
	var ress_id=$(this).attr("data-id");
	var data = new Array();
	var _this=$(this);
	data = [{
				'address_id':ress_id,
        		'name':_this.parents(".siteList-item").find(".data_name").text(),
        		'phone':_this.parents(".siteList-item").find(".data_phone").text(),
        		'region':_this.parents(".siteList-item").find('.data_region').text(),
        		'address':_this.parents(".siteList-item").find('.data_address').text(),
        	}];
	data = JSON.stringify(data);
	cookieSet('saveAddress',data);
	window.location.href="/saveSite.html";
})
$(document).on("click",".confirmAddress",function(){
	var ress_id=$(this).attr("data-id");
	var data = new Array();
	var _this=$(this);
	data = [{
				'address_id':ress_id,
        		'name':_this.find(".data_name").text(),
        		'phone':_this.find(".data_phone").text(),
        		'region':_this.find('.data_region').text(),
        		'address':_this.find('.data_address').text(),
        	}];
	data = JSON.stringify(data);
	cookieSet('confirmAddress',data);
	window.location.href="/orderDetails.html";
})

</script>
</html>